/**
 * @file   1099.cpp
 * @author Shuang Hu <hsmath@ubuntu>
 * @date   Thu Apr 29 04:48:32 2021
 * 
 * @brief  PAT problem 1099:Build a BST!
 * 
 * 
 */
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
class BSTNode{
public:
    int data;
    int left;//Left Child
    int right;
public:
    void setstruct(int l,int r){
	left=l;
	right=r;
    }
    void setdata(int data){
	this->data=data;
    }
};
class BST{
private:
    int nodenum;
    vector<BSTNode> Tree;
    vector<int> dataseq;
public:
    BST(int nodenum);
    void Build(int node);
    void LevelSeq();
};
BST::BST(int nodenum){
    for(int i=0;i<nodenum;i++){
	BSTNode BN;
	int L,R;
	cin>>L>>R;
	BN.setstruct(L,R);
	Tree.push_back(BN);
    }
    for(int i=0;i<nodenum;i++){
	int a;
	cin>>a;
	dataseq.push_back(a);
    }
    sort(dataseq.begin(),dataseq.end());
}
void BST::Build(int node){
    static int i=0;
    if(node==-1){
	return;
    }
    Build(Tree[node].left);
    Tree[node].setdata(dataseq[i++]);
    Build(Tree[node].right);
    return;
}
void BST::LevelSeq(){
    queue<BSTNode> Q;
    Q.push(Tree[0]);
    bool flag=false;
    while(Q.empty()==false){
	BSTNode tmp=Q.front();
	Q.pop();
	if(flag==false){
	    cout<<tmp.data;
	    flag=true;
	}else{
	    cout<<" "<<tmp.data;
	}
	if(tmp.left!=-1){
	    Q.push(Tree[tmp.left]);
	}
	if(tmp.right!=-1){
	    Q.push(Tree[tmp.right]);
	}
    }
    cout<<endl;
}
int main(){
    int N;
    cin>>N;
    BST T(N);
    T.Build(0);
    T.LevelSeq();
}
